/**
 * 模块管理 - 编辑页面
 * @author wenchuan.li
 * @date 2018/12/12 10:35
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            detail: false,
            form: '',
            treeData:[],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            rules: {
                PARENT_NAME: [
                    {required: true, message: '上级模块不能为空', trigger: 'blur'}
                ],
                NAME: [
                    {required: true, message: '模块名称不能为空', trigger: 'blur'}
                ],
                NAME_TW: [
                    {required: true, message: '繁体名称不能为空', trigger: 'blur'}
                ],
                NAME_EN: [
                    {required: true, message: '英文名称不能为空', trigger: 'blur'}
                ],
                IS_AUTO_EXPAND: [
                    {required: true, message: '自动展开不能为空', trigger: 'blur'}
                ],
                IS_ENABLE: [
                    {required: true, message: '是否启用不能为空', trigger: 'blur'}
                ]
            }
        },
        callObj,
        pageVue,layOpenIndex
    ;

    var ModuleEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'module_edit',
                _loadTplsFull: [
                {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
            ]
            }, win);
        },
        editModule: function () {
            app.ajax({
                xurl: 'sys/module/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '模块信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        app.pageModule('module').editNode(result.xdata.ID,result.xdata.NAME);
                        app.pageModule('module').updateTree();
                        layer.close(layOpenIndex);
                        //self._closeTpl();
                    }
                }
            });
        },

        addModule: function () {
            app.ajax({
                xurl: 'sys/module/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '模块信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('module').reloadTableCurr();
                        app.pageModule('module').addNode(result.data.data.node);
                        layer.close(layOpenIndex);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            vueData.detail = false;
            if (obj.is_add) {
                vueData.isAdd=true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        PARENT_ID: obj.module_id,
                        PARENT_NAME: obj.module_name,
                        CASCADE_ID:obj.cascade_id,
                        IS_AUTO_EXPAND: '1',
                        IS_ENABLE: '1'
                    },
                    reset: true
                });
                vueData.treeData = obj.moduleNodeData;
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(obj.detail){
                    vueData.detail = true;
                }else{
                    vueData.detail = false;
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_module']);
            pageVue = new Vue({
                el: '#module_edit',
                data: vueData,
                methods: {

                    openModuleWindow: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'cq_sysmodule_id_1',
                                    searchParam: {
                                        CASCADE_ID: vueData.form.CASCADE_ID
                                    },
                                    columnList: [
                                        {label: '作业编号', prop: 'ID', width: '150'},
                                        {label: '作业名称', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.PARENT_ID = data.ID;
                                    vueData.form.PARENT_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择上级模块',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    cancel: function () {
                        if(vueData.detail){
                            layer.close(layOpenIndex);
                        }else{
                            self._closeTpl();
                        }
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addModule();
                                } else {
                                    self.editModule();
                                }
                            }
                        });
                    },
                    handleSelect:function (item) {
                    },
                    handleNodeClick:function (item) {
                    },
                    querySearch:function (queryString, cb) {
                        
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('module_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new ModuleEdit();

});